package com.tianzeculture.stagebao.activity.artist.fragment;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ListView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.activity.artist.MyArtistActivity;
import com.tianzeculture.stagebao.activity.artist.adapter.PhotoAdapter;
import com.tianzeculture.stagebao.base.BaseFragment;
import com.tianzeculture.stagebao.bean.ArtistsDetailedBean;
import com.tianzeculture.stagebao.bean.PhotoListEvent;
import com.tianzeculture.stagebao.net.UserRestUsage;
import com.tianzeculture.stagebao.network.Request;
import com.tianzeculture.stagebao.network.enity.MSG;
import com.tianzeculture.stagebao.network.util.ToastUtil;
import com.tianzeculture.stagebao.util.Util;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

/**
 * Created by Administrator on 2018/6/7 0007.
 * 艺人详情页-照片
 */

public class MyArtistPhotoFragment extends BaseFragment {

    @BindView(R.id.rvArtist)
    RecyclerView rvArtist;

    PhotoAdapter photoAdapter = null;
    ArtistsDetailedBean bean;
    PhoneBroadcastReceive receive;
    @Request
    UserRestUsage userRestUsage = null;
    private int myInfoId = 55;

    @Override
    public void onAttach(Activity context) {
        super.onAttach(context);
        bean = ((MyArtistActivity) context).getData();
    }

    @Override
    public void refreshUI(int taskId, MSG msg) {
        super.refreshUI(taskId, msg);
        if (taskId == myInfoId) {
            if (msg.getData()) {
                bean = (ArtistsDetailedBean) msg.getObj();
                photoAdapter.setList(bean.getListp());
            }
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void initUI() {
        super.initUI();
        EventBus.getDefault().register(this);
        receive = new PhoneBroadcastReceive();
        IntentFilter intentFiltet = new IntentFilter();
        //设置广播的名字（设置Action，可以添加多个要监听的动作）
        intentFiltet.addAction("myPhoneBroadCastAction");
        getActivity().registerReceiver(receive, intentFiltet);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvArtist.setLayoutManager(layoutManager);
        rvArtist.setHasFixedSize(true);
        photoAdapter = new PhotoAdapter(getActivity(), bean.getListp(), true);
        rvArtist.setAdapter(photoAdapter);
    }

    @Override
    protected int getLayoutResID() {
        return R.layout.fragment_artist_home;
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PhotoListEvent event) {
        userRestUsage.myInfo(myInfoId, Util.UserId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(receive);
    }


    class PhoneBroadcastReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("myPhoneBroadCastAction".equals(intent.getAction()))
                photoAdapter.notifyDataSetChanged();
        }
    }
}
